import { sqlite } from "./index.js";

const tableName = 'sys_avatar'

const fields = `"worker_id" INTEGER NOT NULL,"img_url" text`
/**
* @worker_id Int
* @img_url String
**/
	 
export const SysAvatar = {
	/**
	 * 初始化
	 **/
	init: function(){
		return new Promise(async(resolve, reject) => {
			if(!sqlite.isOpen()){
				await sqlite.openDb();
			}
			sqlite.isTable(tableName).then(res => {
				if(res) {
					resolve({status: 1}); 
				}else {
					sqlite.createTab(tableName, fields);
					resolve({msg: 'init', status: 0}); 
				}
			}).catch(err => {
				console.log('读取表是否存在失败', err);
				reject(err); 
			})
		})
	},
	/**
	 * 查询
	 **/
	select: function(id) {
		return sqlite.selectBySQL(`select * from '${tableName}' where worker_id = ${id};`);
	},
	/**
	 * 新增
	 **/
	insert: function(data){
		return sqlite.addTabItem(tableName, data);
	},
	/**
	 * 修改
	 **/
	updateById: function(id, key, keyValue) {
		let sql = `update ${tableName} set ${key} = '${keyValue}' where worker_id = ${id}`;
		return sqlite.updateBySQL(sql);
	},
	/**
	 * 删除
	 **/
	deleteById: function(id){
		return sqlite.deleteInformationType(tableName, {'worker_id': id});
	},
}
	 